package com.wf.demo.job.service;

import com.wf.demo.info.pojo.Alarm;
import com.wf.demo.info.pojo.Device;
import com.wf.demo.info.service.DeviceService;
import com.wf.demo.info.util.Page4Navigator;
import com.wf.demo.job.dao.DataDeviceDAO;
import com.wf.demo.job.pojo.DataDevice;
import com.wf.demo.job.pojo.LocationInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import javax.xml.stream.Location;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * @Auther: wf
 * @Date: 2019/11/25 16:53
 * @Description:
 */
@Service
public class DataDeviceService {
    @Autowired
    DataDeviceDAO dataDeviceDAO;
    @Autowired
    DeviceService deviceService;
    //单个设备的所有数据
    public Page4Navigator<DataDevice> list(Long did, int start, int size, int navigatePages){
        Device device=deviceService.get(did);
        Sort sort = new Sort(Sort.Direction.DESC, "id");  //id倒序排序，最新的记录在最前面
        Pageable pageable = new PageRequest(start, size, sort);
        Page pageFromJPA = dataDeviceDAO.findByDevice(device,pageable);
        return new Page4Navigator<>(pageFromJPA,navigatePages);
    }
    //得到所有数据最新的20个  size=20
    public List<DataDevice> getTotalLatestData(int size){
        Sort sort = new Sort(Sort.Direction.DESC, "id");  //id倒序排序，最新的记录在最前面
        Pageable pageable = new PageRequest(0, size, sort);
        Page<DataDevice> page=dataDeviceDAO.findAll(pageable);
        return page.getContent();
    }

    //增删改查
    public void add(DataDevice bean){
        dataDeviceDAO.save(bean);
    }
    public void update(DataDevice bean){
        dataDeviceDAO.save(bean);
    }
    public void delete(Long id){
        dataDeviceDAO.delete(id);
    }
    public DataDevice get(Long id){
        return dataDeviceDAO.findOne(id);
    }

    public  void addData( List<BigDecimal> dataValues, List<String> infoString,Device device){
        BigDecimal[] bs=dataValues.toArray(new BigDecimal[dataValues.size()]); //数据数组方便
        DataDevice dataPojo= new DataDevice();  //数据类型的实体
        dataPojo.setDevice(device); //存入device到dataPojo；

        try{
            String status = infoString.get(0); //运行状态字符串
            System.out.println("DATA数据里面的status"+status);
            String scrapCode = infoString.get(1);//故障码字符串
            System.out.println("DATA数据里面的scrapCode"+scrapCode);
            dataPojo.setLongitude(bs[0]);
            dataPojo.setLatitude(bs[1]);
            dataPojo.setSpeed(bs[2]);
            dataPojo.setStatus(status);    //运行状态字符串
            dataPojo.setGreasePressure(bs[4]);
            dataPojo.setWaterTemp(bs[5]);
            dataPojo.setAlarmCode(scrapCode);//故障码字符串
            //会发生错误的地方
            //对于解析表新增的数据类型，
            dataPojo.setData1(bs[7]);
            dataPojo.setData2(bs[8]);
            dataPojo.setData3(bs[9]);
            dataPojo.setData4(bs[10]);
            dataPojo.setData5(bs[11]);
            dataPojo.setData6(bs[12]);
            dataPojo.setData7(bs[13]);
        }
        catch(Exception e){
            System.out.println("新增的数据类型在北斗传来的信息中不存在");
            e.printStackTrace();
        }
        finally{
            dataDeviceDAO.save(dataPojo);
            System.out.println("无论新增的数据是否存在， 都会执行的代码");
        }
    }
    //查找设备最新的数据集合，可指定有几个
    public  List<DataDevice> getLatestData (Device device,int size){
        Sort sort = new Sort(Sort.Direction.DESC, "id");  //id倒序排序，最新的记录在最前面
        Pageable pageable = new PageRequest(0, size, sort);
        Page<DataDevice> page= dataDeviceDAO.findByDevice(device,pageable);
        List<DataDevice> dataDevices =page.getContent();
        /*DataDevice d=dataDevices.get(0); //0位最新的一个
        System.out.println(d.getCreateTime());*/
        return  dataDevices;
    }
    //去掉data里面的设备信息
    public void removeDeviceFromData(Device device){
        List<DataDevice> dataDevices= device.getDataDevices(); //得到device下的data集合
        if(null != dataDevices){
            for(DataDevice dataDevice :dataDevices){
                dataDevice.setDevice(null);
            }
        }
    }
    public void removeDeviceFromData(List<Device> ds) {
        for(Device device:ds){
            removeDeviceFromData(device);
        }
    }
    //对data去掉device信息
    public void removeDeviceFromDataNew(List<DataDevice> dataDevices){
        for(DataDevice dataDevice:dataDevices){
            dataDevice.setDevice(null);
        }
    }
    /**
     *功能描述  轨迹运动的变化，默认传输24个点，每个点是间隔一个小时的位置数据
     * @author wf
     * @date 2019/12/17
     * @param device
     *  @param size
     * @return java.util.List<com.wf.demo.job.pojo.LocationInfo>
     */
    public  List<LocationInfo> getLocationChange(Device device ,int size,int pageNum){
        //得到设备的所有数据page ，每个page页数大小为30个，即2*30为1个小时的数据，取24页
        List<LocationInfo> locationInfos=new ArrayList<>();
        Sort sort = new Sort(Sort.Direction.ASC,"id");
        //按顺序得到24页数据，取每页的第一个
       for(int pageNumber=0;pageNumber<pageNum;pageNumber++){
            Pageable pageable = new PageRequest(pageNumber,size,sort);
            Page<DataDevice> page=dataDeviceDAO.findByDevice(device,pageable);
            //得到每页的第一个
           if( ! page.getContent().isEmpty()){
               DataDevice dataDevice= page.getContent().get(0);
               LocationInfo  locationInfo=  new LocationInfo();
              // System.out.println(dataDevice.getId());
               //将经纬度读出赋值给 locationInfo
               locationInfo.setLongitude(dataDevice.getLongitude());
               locationInfo.setLatitude(dataDevice.getLatitude());
               //添加到集合中
               locationInfos.add(locationInfo);
           }
       }
        return  locationInfos;
    }
    //查找设备最新的数据集合，可指定有几个
    /**
     *功能描述  根据起始和结束时间查找某个设备在该时间段的所有数据
     * @author wf
     * @date 2020/9/14
      * @param
     * @return
     */
    public  List<DataDevice> getDeviceDataByTime (Long did, LocalDateTime startTime,LocalDateTime endTime){
        Sort sort = new Sort(Sort.Direction.DESC, "id");  //id倒序排序，最新的记录在最前面
        Device device=deviceService.get(did);
        List<DataDevice> dataDevices =dataDeviceDAO.findAllByDeviceAndCreateTimeBetween(device,startTime,endTime);
        return  dataDevices;
    }
}
